﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace IceScream.Common
{
    public static class UICommon
    {

        public static string Encryption(string sourceString)
        {
            sourceString = sourceString.TrimEnd();

            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(sourceString));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool Decoding(string input, string sourceString)
        {
            string hashOfInput = Encryption(input);

            return hashOfInput == sourceString;
        }

        public static string ChangeFileName(string[] FileList, string currentFile)
        {
            foreach (var fileDir in FileList)
            {
                int i = 0;
                string[] FileName = fileDir.Split('\\');
                if (FileName.ElementAt(FileName.Count() - 1) == currentFile)
                {
                    string[] Name = currentFile.Split('.');
                    currentFile = Name[0] + "_" + i + "." + Name[1];
                    ChangeFileName(FileList, currentFile);
                }
            }
            return currentFile;
        }
    }
}